package com.info3k.client.test;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import com.info3k.client.domain.User;
import com.info3k.client.utils.ToolsClient;
import com.info3k.server.utils.Tools;

/* NAPOMENA: Nismo u mogucnosti da testiramo persistence metode jer nije 
 * 			 moguce iz junit testa pristupiti app-engine bazi, a klijent 
 * 			 stranu i nema bas puno smisla testirati junit testovima, pa smo se
 * 			 odlucili da pravimo junit testove samo za neke specificne metode.
*/

public class Info3KTestCase extends TestCase {
	public void testConvertingArrayToCollection(){
		List<User> listUsers = new ArrayList<User>();
		listUsers.add(createUser());
		listUsers.add(createUser());
		listUsers.add(createUser());
		
		User[] users = ToolsClient.fromCollectionToArray(listUsers, new User[listUsers.size()]);
		for (int i = 0; i < users.length; i++) {
			assertEquals(users[i], listUsers.get(i));
		}
		assertEquals(users.length, listUsers.size());
	}
	
	public void testConvertingCollectionToArray(){
		User[] users = new User[3];
		users[0] = createUser();
		users[1] = createUser();
		users[2] = createUser();
		
		List<User> listUsers = ToolsClient.fromArrayToCollection(users);
		
		for (int i = 0; i < listUsers.size(); i++) {
			assertEquals(users[i], listUsers.get(i));
		}
		assertEquals(users.length, listUsers.size());
	}
	
	public void testMD5(){
		String md51 = Tools.getMD5("info3k");
		String md52 = "ddbadcc46a76f69d9ceecc0cb167bf4b";
		assertEquals(md51, md52);
	}
	
	public void testRandomString(){
		String randomString = Tools.getRandomString(8);
		assertEquals(8, randomString.length());
	}
	
	public void testEmailAddress(){
		String email = "email@email.com";
		String notEmail = "email@email.com";
		assertTrue(ToolsClient.isEmail(email));
		assertFalse(ToolsClient.isEmail(notEmail));
	}
	
	private User createUser(){
		User u = new User();
		u.setUsername("user" + System.currentTimeMillis());
		u.setEmail("email" + System.currentTimeMillis());
		u.setId(System.currentTimeMillis());
		u.setPhone("phone" + System.currentTimeMillis());
		u.setSurname("surname" + System.currentTimeMillis());
		u.setName("name" + System.currentTimeMillis());
		u.setPassword("password" + System.currentTimeMillis());
		return u;
	}
}